import { CascaderOption } from "element-plus";

/** 性别 */
export enum filterSex {
   "PRIVACY" = "保密",
   "MAN" = "男",
   "WOMAN" = "女"
}

/** 支付状态 */
export enum filterPayStatus {
   "CLOSE" = "关闭",
   "PAID" = "已支付",
   "OPEN" = "开启未创建",
   "NOT_PAID" = "创建未支付",
   "CANCEL" = "取消支付"
}

/** 客户状态 */
export enum filterClientStatus {
   "NORMAL" = "正常",
   "LOGINOUT" = "注销"
}

/** 格式化金额 */
export function filterExpenses(fee?: number | string | null) {
   if (fee) {
      try {
         if (typeof fee == "string") fee = Number(fee);

         return fee / 100 + " 元";
      } catch (error) {
         return "";
      }
   } else {
      return "";
   }
}

/** 管理端用户状态 */
export enum filterAdminUserStatus {
   "NORMAL" = "正常",
   "LOGINOUT" = "注销",
   "NOT_ACTIVATED" = "未激活"
}

/** 通用状态 */
export enum GeneralStatusFilter {
   "OPEN" = "开启",
   "CLOSE" = "关闭"
}

/** 通用状态 type */
export enum GeneralStatusTypeFilter {
   "OPEN" = "",
   "CLOSE" = "danger"
}

interface Tree {
   [key: string]: any;
   children?: Tree[];
}

interface FormatTreeOption {
   /** 指定数据值的字段名称 */
   value?: string;
   /** 指定显示名称的字段名称 */
   label?: string;
   /** 指定是否禁用的字段名称 */
   disabled?: string;
   children?: string;
   /** 指定数据的 key */
   valueName?: string;
   /** 指定显示名称的 key */
   labelName?: string;
}

/** 格式化树结构 */
export function formatTree<T = any>(tree?: Tree[], options?: FormatTreeOption): T[] | undefined {
   if (!tree) return undefined;
   const newTree = tree.map(item => {
      if (item[options?.children || "children"] && item[options?.children || "children"]?.length > 0) {
         const children = formatTree(item[options?.children || "children"], options);

         return {
            [options?.labelName || "label"]: item[options?.label || "label"] as string,
            [options?.valueName || "value"]: item[options?.value || "value"] as string,
            disabled: item[options?.disabled || "disable"] as boolean,
            children
         };
      } else {
         return {
            [options?.labelName || "label"]: item[options?.label || "label"] as string,
            [options?.valueName || "value"]: item[options?.value || "value"] as string,
            disabled: item[options?.disabled || "disable"] as boolean
         };
      }
   });

   return <T[]>newTree;
}
